﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using UcAsp.Models;

namespace UcAsp.WorkFrame
{
    public class SettingConfig
    {
        private static string path = AppDomain.CurrentDomain.BaseDirectory;
        /// <summary>
        /// 获取网站设置
        /// </summary>
        public static Models.SiteSetting GetSiteSetting
        {
            get
            {
               
               
                    SiteSetting model = new Models.SiteSetting();

                    XmlDocument xmlDoc = new XmlDocument();
                    xmlDoc.Load(path + "\\Content\\config\\SiteSettings.xml");
                    string xmlpath = ("Settings");
                    if (xmlDoc.SelectNodes(xmlpath).Count != 0)
                    {
                        XmlNodeList xmlList = xmlDoc.SelectNodes(xmlpath);
                        foreach (XmlNode xns in xmlList)
                        {
                            foreach (XmlNode xn in xns)
                            {

                                for (int i = 0; i < xn.ChildNodes.Count; i++)
                                {
                                   Parser.XmlToObject(model, xn.Name, xn.InnerText);
                                }

                            }
                        }
                    }
                    
                    return model;
                }

            }
     
        /// <summary>
        /// 保存网站设置
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static bool SaveSiteSetting(SiteSetting model)
        {

            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(path + "\\Content\\config\\SiteSettings.xml");
            string xmlpath = "Settings";
            XmlNode root = xmlDoc.SelectSingleNode(xmlpath);
            Type type = model.GetType();
            System.Reflection.PropertyInfo[] propertys = type.GetProperties();
            foreach (System.Reflection.PropertyInfo t in propertys)
            {
                XmlElement node = (XmlElement)root.SelectSingleNode(t.Name);
                System.Reflection.PropertyInfo property = type.GetProperty(t.Name);
                //当model值为空是跳过
                try
                {
                    node.InnerText = property.GetValue(model, null).ToString().Replace("../","/");;
                }
                catch { }
            }
            xmlDoc.Save(path + "\\Content\\config\\SiteSettings.xml");

            return true;
        }
 
    }
}
